package dryworks.jsf.parsers;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ParsersTrics {
	public static String chopFirstDoctype(String in) {
		String s = "<!DOCTYPE";
		int place = in.indexOf(s);
		if (place < 0)
			return in;
		String head = in.substring(0, place);
		String res = in.substring(place + s.length());
		place = res.indexOf("<");
		if (place < 0) {
			return in;
		}
		return head + res.substring(place);
	}

	public static String stream2string(InputStream in) throws IOException {
		StringBuffer res = new StringBuffer();
		int c;
		while ((c = (int)in.read()) != -1) {
			res.append((char)c);
		}
		return res.toString();
	}
	
	public static InputStream chopFirstDoctype(InputStream in) throws IOException {
		String temp = stream2string(in);
		temp = chopFirstDoctype(temp);
		ByteArrayInputStream res = new ByteArrayInputStream(temp.getBytes());
		return res;
	}
	
	public static void main(String[] args) {
		try {
			File file = new File("meta/META-INF/bnhp.tld");
			
			if (file.exists()) {
				System.out.println("exist");
			} else {
				System.out.println("not exist");
			}
			String content = ParsersTrics.stream2string(new FileInputStream(file));
			System.out.println(content);
			String res = ParsersTrics
					.chopFirstDoctype("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?><!DOCTYPE taglib  PUBLIC \"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN\" \"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd\"><taglib>");

			System.out.println("res:" + res);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
